package com.farm.cbtg.controller;

import com.farm.cbtg.domain.Appcatalog;
import com.farm.cbtg.domain.ex.CatalogDomain;
import com.farm.cbtg.service.AppcatalogServiceInter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;

import com.farm.web.easyui.EasyUiTreeNode;
import com.farm.web.easyui.EasyUiUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpSession;
import com.farm.core.page.RequestMode;
import com.farm.core.page.OperateType;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.page.ViewMode;
import com.farm.web.WebUtils;

/* *
 *功能：业务分类控制层
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@RequestMapping("/catalogtree")
@Controller
public class AppcatalogTreeController extends WebUtils {
	private final static Logger log = Logger.getLogger(AppcatalogTreeController.class);
	@Resource
	private AppcatalogServiceInter appCatalogServiceImpl;

	/**
	 * 业务分类树
	 */
	@RequestMapping("/catalogtree")
	@ResponseBody
	public Object examtypeTree(String id, String funtype, String domain, HttpSession session) {
		try {
			CatalogDomain domainObj = CatalogDomain.getDomain(domain);
			String domainRule = "";
			if (StringUtils.isNotBlank(domain)) {
				domainRule = " and a.domain='" + domain + "'";
			}
			if (id == null) {
				// 如果是未传入id，就是根节点，就构造一个虚拟的上级节点
				id = "NONE";
				List<EasyUiTreeNode> list = new ArrayList<>();
				EasyUiTreeNode nodes = new EasyUiTreeNode("NONE", domainObj.getTitle(), "open", "icon-customers");
				nodes.setChildren(EasyUiTreeNode.formatAsyncAjaxTree(
						EasyUiTreeNode.queryTreeNodeOne(id, "SORT", "CBTG_APP_CATALOG", "ID", "PARENTID", "NAME",
								"CTIME", domainRule).getResultList(),
						EasyUiTreeNode.queryTreeNodeTow(id, "SORT", "CBTG_APP_CATALOG", "ID", "PARENTID", "NAME",
								"CTIME", domainRule).getResultList(),
						"PARENTID", "ID", "NAME", "CTIME"));
				list.add(nodes);
				return list;
			} else {
				List<EasyUiTreeNode> list = new ArrayList<>();
				list = EasyUiTreeNode.formatAsyncAjaxTree(
						EasyUiTreeNode.queryTreeNodeOne(id, "SORT", "CBTG_APP_CATALOG", "ID", "PARENTID", "NAME",
								"CTIME", domainRule).getResultList(),
						EasyUiTreeNode.queryTreeNodeTow(id, "SORT", "CBTG_APP_CATALOG", "ID", "PARENTID", "NAME",
								"CTIME", domainRule).getResultList(),
						"PARENTID", "ID", "NAME", "CTIME");
				return list;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	@RequestMapping("/treeView")
	public ModelAndView forSend(String ids, String funtype, String domain) {
		CatalogDomain domainObj = CatalogDomain.getDomain(domain);
		return ViewMode.getInstance().putAttr("ids", ids).putAttr("funtype", funtype).putAttr("domain", domainObj)
				.returnModelAndView("cbtg/AppcatalogChooseTreeWin");
	}
}
